<?php


namespace app\admin\controller;


use app\admin\base\JwBase;
use app\model\Admin;
use think\Exception;
use think\facade\Validate;

class Login extends JwBase
{
    public function index()
    {
        try {
            if (request()->isPost()) {
                $postData = input('post.');
                $rule = [
                    'account|账号' => ['require', 'alphaNum', 'max' => 15, 'min' => 6],
                    'password|密码' => ['require', 'alphaNum', 'max' => 15, 'min' => 6]
                ];
                $validate = Validate::rule($rule);
                if (!$validate->check($postData)) {
                    $this->respInfo(2500, [], $validate->getError());
                }

                $data['account'] = $postData['account'];
                $data['password'] = set_password($postData['password']);

                $admin = Admin::where($data)->find();
                if ($admin) {
                    if($admin->status == 2){
                        $this->respInfo(3002);
                    }
                    $admin->login = $admin->login + 1;
                    $admin->lasttime = date("Y-m-d H:i:s");
                    $admin->save();
                    $re = ['token' => $this->getJwt($admin), "account" => $postData['account']];
                    $this->respInfo(1000, $re);
                } else {
                    $this->respInfo(3000);
                }
            } else {
                $this->respInfo(2001);
            }

        } catch (Exception $e) {
            $this->respInfo(2002);
        }
    }


}